﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using Web.Framework;
using System.Web.Security;
using System.Web;
using CrmContacto.Contracts;
using Web.CrmContacto.Code;
using Web.Framework.Translate;
using CrmContacto.WebApp.Code.Sap;
using Web.CrmContacto.Code.Sap;
using CrmContacto.Contracts.Contracts.Search;
using CrmContacto.Contracts.Enums;
using Web.Framework.Validation;

namespace Web.CrmContacto.Svc
{
    [ServiceContract(Namespace = "")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class WebAjaxCrm
    {
        [OperationContract]
        [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json)]
        public void dummuy()
        {
        }

        [OperationContract]
        [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json)]
        public List<MaestroBase> GetProvincias(int paisId, bool includeDefault)
        {
            List<MaestroBase> returnList = new List<MaestroBase>();

            if (includeDefault)
            {
                returnList.Add(new MaestroBase() { Id = Convert.ToInt32(UIHelper.cboNullValue), Nombre = LocalizationProvider.Instance["DropDown.SinProvincia"] });
            }

            if (paisId != -1)
            {
                returnList.AddRange(WebServiceHelper.GetProvincias(paisId));
            }

            return returnList;
        }

        [OperationContract]
        [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json)]
        public List<MaestroBase> GetPuntosDeVenta(int filtroOutlet, bool includeDefault)
        {
            List<MaestroBase> returnList = new List<MaestroBase>();

            if (includeDefault)
            {
                returnList.Add(new MaestroBase() { Id = Convert.ToInt32(UIHelper.cboNullValue), Nombre = LocalizationProvider.Instance["DropDown.SinProvincia"] });
            }
            PuntoDeVentaSearch pdvSearch = new PuntoDeVentaSearch();
            pdvSearch.EmpresaId = BasePage.CompanyId;

            if (filtroOutlet != -1)
            {
                TipoPdv tipoPdv = (TipoPdv)filtroOutlet;
                pdvSearch.Outlet = tipoPdv == TipoPdv.Outlet;
            }

            var pdvList = WebServiceHelper.GetPuntoDeVentaDto(pdvSearch);

            List<MaestroBase> pdvMaestroBaseList = pdvList.Select(p => new MaestroBase() { Id = p.Id, Nombre = p.DisplayName }).ToList();
            returnList.AddRange(pdvMaestroBaseList);

            return returnList;
        }

        /// <summary>
        /// Este método se usaba para el autocomplete de la busqueda de productos, fue reemplazado por el iframe multiseleccion
        /// </summary>
        /// <param name="searchTerm"></param>
        /// <returns></returns>
        //[OperationContract]
        //[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json)]
        //public List<SapCodigoProducto> GetProductosFromSap(string searchTerm)
        //{
        //    List<SapCodigoProducto> returnList = new List<SapCodigoProducto>();

        //    SAPWebServiceHelper wsHelper = new SAPWebServiceHelper();
        //    returnList = wsHelper.ZWS_CHECK_MATNR_PORPARTE(searchTerm);

        //    return returnList;
        //}

        [OperationContract]
        [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json)]
        public bool ValidarEmail(string emailaddress)
        {
            RegexUtility u = new RegexUtility();
            return u.IsValidEmail(emailaddress);
        }
    }
}
